package mylab.usm.oauth2.server.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.fujieid.jap.ids.model.ClientDetail;
import com.fujieid.jap.ids.model.enums.GrantType;
import com.fujieid.jap.ids.model.enums.ResponseType;
import com.fujieid.jap.ids.provider.IdsScopeProvider;
import com.fujieid.jap.ids.service.IdsClientDetailService;
import com.fujieid.jap.ids.util.OauthUtil;
import com.xkcoding.http.util.StringUtil;
import lombok.RequiredArgsConstructor;
import mylab.usm.api.model.account.App;
import mylab.usm.api.service.AppService;
import mylab.usm.oauth2.server.utils.PojoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 客户端信息crud
 *
 * @author lapati5
 * @date 2021/8/4
 */
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class IdsClientDetailServiceImpl implements IdsClientDetailService {

    final static String DELIMITER = " ";

    private final AppService appService;


    @Override
    public ClientDetail getByClientId(String clientId) {
        return PojoUtil.clientFromEntity(appService.getByClientId(clientId));
    }

    @Override
    public ClientDetail add(ClientDetail clientDetail) {
        //生成clientId和ClientSecret
        clientDetail.setClientId(IdUtil.fastSimpleUUID())
                .setClientSecret(OauthUtil.generateClientSecret())
                .setAvailable(true);
        //默认允许获取所有信息
        if (StringUtil.isEmpty(clientDetail.getScopes()) || clientDetail.getScopes().length() == 0) {
            clientDetail.setScopes(String.join(" ", IdsScopeProvider.getScopeCodes()));
        }
        //默认适用所有授权模式
        if (StringUtil.isEmpty(clientDetail.getGrantTypes()) || StringUtil.isEmpty(clientDetail.getResponseTypes())) {
            clientDetail.setGrantTypes(String.join(DELIMITER, GrantType.grantTypes()));
            clientDetail.setResponseTypes(String.join(DELIMITER, Arrays.asList(ResponseType.CODE.getType(), ResponseType.TOKEN.getType(), ResponseType.ID_TOKEN.getType(), ResponseType.NONE.getType())));
        }
        //存入数据库中
        App foo = appService.convert(BeanUtil.beanToMap(clientDetail));
        appService.add(foo);
        clientDetail.setId(foo.getId().toString());

        System.out.println("client detail: " + clientDetail.getAppName() + ", " + clientDetail.getClientId());
        return clientDetail;
    }

    @Override
    public ClientDetail update(ClientDetail clientDetail) {
        App foo = appService.convert(BeanUtil.beanToMap(clientDetail));
        appService.update(foo);
        return getByClientId(foo.getClientId());
    }

    @Override
    public boolean removeById(String id) {
        return appService.removeById(id);
    }

    @Override
    public boolean removeByClientId(String clientId) {
        return appService.deleteByClientId(clientId);
    }

    @Override
    public List<ClientDetail> getAllClientDetail() {
        List<App> list = appService.getAll();
        return list.stream().map(PojoUtil::clientFromEntity).collect(Collectors.toList());
    }
}
